<?php
/*
 * Created by PhpStorm.
 * Date: 16/9/28
 * Time: 上午10:02
 * User: liubin
 */

namespace api\Model;

use Think\Model;

class DiaryEntityModel extends Model
{
    function __construct()
    {
        $this->model  = M('DiaryEntity');
        $this->model1 = M('DiaryResponse');
        $this->model2 = M('CustEntity');
        $this->model3 = M('DiaryFabulous');
        $this->model4 = M('CustFans');
        $this->model5 = M('DiaryRecomment');
        $this->model6 = M('InfoEntity');
        $this->model7 = M('InfoResponse');
        $this->model8 = M('InfoFabulous');
    }

    public function getDiary($order, $where = '', $page = 1, $pergage = 10)
    {
        $data = $this->model->where("1=1 " . $where)->page($page, $pergage)->order("" . $order . " desc")->select();
        foreach ($data as $key=>$val){
            $data[$key]['allnum'] = $this->model->where("1=1 " . $where)->count();
        }
        $data = $data ? $data : array();
        return $data;
    }



    //资讯列表
    public function zixunlist($page = 1, $pergage = 10)
    {
        $data = $this->model6->page($page, $pergage)->order('add_at desc')->select();
        foreach ($data as $key=>$val){
            $data[$key]['allnum'] = $this->model6->count();

            $data[$key]['response'] = $this->model7->where('info_id='.$val['info_id'])->count();
        }
        $data = $data ? $data : array();
        return $data;
    }


    //资讯详情
    public function getzixundetails($did)
    {
        $data = $this->model6->field('*')->where('info_id=' . $did)->find();
        return $data;
    }

    //资讯评论
    public function getzxping($did,$page = 1, $pergage = 10)
    {
        $data = $this->model7->where('info_id='.$did)->page($page,$pergage)->order('add_at desc')->select();
        foreach ($data as $key=>$value){
            $data[$key]['nickname'] = $this->model2->where('cust_id=' . $value['cust_id'])->getField('nickname');
            $data[$key]['cust_img'] = $this->model2->where('cust_id=' . $value['cust_id'])->getField('cust_img');
        }
        $data = $data ? $data : array();
        return $data;
    }


    //我的关注日记
    public function getDiary_guan($where,$cust_id, $page = 1, $pergage = 10)
    {
        $guan = $this->model4->where("fans_id='".$cust_id."' and type=0")->order('add_at desc')->select();
        foreach ($guan as $k=>$v){
            $between[]=$v['cust_id'];
        }
        $between=implode(',',$between);
        if($between){
            $data = $this->model->where("cust_id in(".$between.") ".$where)->page($page, $pergage)->order("add_at desc")->select();
            foreach ($data as $key=>$val){
                $data[$key]['allnum'] = $this->model->where("cust_id in(".$between.") ".$where)->count();
            }
        }
        $data = $data ? $data : array();
        return $data;
    }

    //首页推荐日记
    public function gettui_Diary($page = 1, $pergage = 10)
    {
        $guan = $this->model5->page($page, $pergage)->order('sort asc')->select();
        foreach ($guan as $k=>$v){
            $diarys = $this->model->where("diary_id =".$v['diary_id'])->find();
            if($diarys){
                $data[]=$diarys;
            }
        }
        $data = $data ? $data : array();
        return $data;
    }


    public function getDiaryrep($field = array('*'), $condition = array()) {
        //print_r($condition);die;
        $data = $this->model1->where($condition)->field($field)->find();
        $data = $data ? $data : array();
        return $data;
    }

    public function getDiaryrep_zixun($condition = array()) {
        //print_r($condition);die;
        $data = $this->model7->where($condition)->find();
        $data['nickname'] = $this->model2->where('cust_id=' . $data['cust_id'])->getField('nickname');
        $data['cust_img'] = $this->model2->where('cust_id=' . $data['cust_id'])->getField('cust_img');
        $data = $data ? $data : array();
        return $data;
    }

    public function diaryCommentnum($did)
    {
        $count = $this->model1->where("diary_id='" . $did . "'and response_id=0")->count();
        return $count;
    }


    public function get_child($diary_id,$id,$cust_id,$page,$perpage){
        $arr = array();
        $rows = M('DiaryResponse')
            ->where("diary_id ='".$diary_id."' and response_id = '".$id."'")->field('id,diary_id,response_id,response_cust_id,object_cust_id,content,add_at')->page($page,$perpage)->order('add_at desc')->select();
        if($rows){
            foreach($rows as $k=>$v){
                $response_info = S('cust_msg_'.$v['response_cust_id']);
                if(!$response_info){
                    $response_info = $this->model2->where('cust_id=' . $v['response_cust_id'])->field('nickname,cust_img')->find();
                }
                $or_guan=0;
                if($cust_id){
                    $or_guan = $this->model4->where("cust_id='".$v['response_cust_id']."' and fans_id='".$cust_id."' and type=0")->getField('id');
                }
                $cust_info = S('cust_msg_'.$v['object_cust_id']);
                if(!$cust_info){
                    $cust_info = $this->model2->where('cust_id=' . $v['object_cust_id'])->field('nickname,cust_img')->find();
                }
                $rows[$k]['response_name']=$response_info['nickname'];
                $rows[$k]['response_img']=$response_info['cust_img'];
                if($or_guan>0){
                    $rows[$k]['or_guan']=1;
                }else{
                    $rows[$k]['or_guan']=0;
                }
                $rows[$k]['object_cust_name']=$cust_info['nickname'];
                $rows[$k]['cust_img']=$cust_info['cust_img'];
                $rows[$k]['list'] = $this->get_child($diary_id,$v['id'],$cust_id);
                //$arr[] = $rows;//组合数组
            }
            return $rows;
        }
    }


    public function diaryComment($cust_id,$did,$page = 1, $perpage = 10)
    {
        $info = $this->model1->where("diary_id='" . $did . "' and response_id=0")->order('add_at asc')->page($page,$perpage)->select();
        //echo $this->model1->_sql();die;
        foreach($info as $key=>$val){
            $ins=$this->get_child($val['diary_id'],$val['response_id'],$cust_id,$page,$perpage);
        }
        return $ins;
    }

    public function getDiarydetails($did)
    {
        $data = $this->model->field('*')->where('diary_id=' . $did)->find();
        return $data;
    }

    //添加日记评论
    public function addOneComment($param = array())
    {
        $insert_id = $this->model1->data($param)->add();
        return $insert_id;
    }

    //添加日记评论
    public function addOneComment_zixun($param = array())
    {
        $insert_id = $this->model7->data($param)->add();
        return $insert_id;
    }


    //日记点赞
    public function diaryzan($param = array())
    {
        $affected = $this->model3->where("cust_id='".$param['cust_id']."' and diary_id='".$param['diary_id']."'")->find();
        if($affected){
            return '-1';
        }
        $insert_id = $this->model3->data($param)->add();
        if($insert_id){
            $diaryinfo = $this->model->where("diary_id='".$param['diary_id']."'")->find();
            $zanNum=$diaryinfo['support']+1;
            $affected1 = $this->model->where("diary_id='".$param['diary_id']."'")->save(array('support'=>$zanNum));
        }
        return $insert_id;
    }

    //日记是否点赞
    public function diaryorzan($param = array())
    {
        $affected = $this->model3->where("cust_id='".$param['cust_id']."' and diary_id='".$param['diary_id']."'")->find();
        if($affected){
            return '-1';
        }else{
            return '1';
        }
    }

    //资讯点赞
    public function zixunzan($param = array())
    {
        $affected = $this->model8->where("cust_id='".$param['cust_id']."' and info_id='".$param['info_id']."'")->find();
        if($affected){
            return '-1';
        }
        $insert_id = $this->model8->data($param)->add();
        if($insert_id){
            $diaryinfo = $this->model6->where("info_id='".$param['info_id']."'")->find();
            $zanNum=$diaryinfo['support']+1;
            $this->model6->where("info_id='".$param['info_id']."'")->save(array('support'=>$zanNum));
        }
        return $insert_id;
    }

    //资讯是否点赞
    public function zixunorzan($param = array())
    {
        $affected = $this->model8->where("cust_id='".$param['cust_id']."' and info_id='".$param['info_id']."'")->find();


        if($affected){
            return '-1';
        }else{
            return '1';
        }
    }


    //删除日记
    public function deletediary($condition = array())
    {
        $delete_id = $this->model->where($condition)->delete();
        if($delete_id){
            $info=$this->model1->where($condition)->find();
            if($info){
                $this->model1->where($condition)->delete();
            }
        }
        return $delete_id;
    }



    //删除资讯评论
    public function deletezxping($condition = array())
    {
        $delete_id = $this->model7->where($condition)->delete();
        //echo $this->model7->_sql();die;
        return $delete_id;
    }



    //添加一篇日记
    public function addOneDiary($param = array())
    {
        $insert_id = $this->model->data($param)->add();
        return $insert_id;
    }

    public function updateOneDiary($param = array(), $condition = array())
    {
        $affected = $this->model->where($condition)->setField($param);
        return $affected;
    }

    public function saveOneDiary($param = array())
    {
        $insert_id = $this->model->data($param)->add();
        return $insert_id;
    }

}
